<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
class OrderController extends \App\Http\Controllers\AdminController{
    //put your code here
    public function index(Request $request) {
        $orders = \App\Models\Order::orderBy('status', 'asc')->orderBy('time', 'desc');
        if ($request->has('status')) {
            $orders->where('status', $request->input('status'));
        }
        $orders = $orders->paginate();
        $msg = '';
        if (!count($orders)) {
            $msg = '没有该种类订单';
        }
        $plots = \App\Models\Plot::where('status', 1)->get();
        $append = $request->all();
        $assign = compact('orders', 'append', 'msg', 'plots', 'request');
        return view('admin.order.index', $assign);
    }
    /**
     * 查找订单
     * @param Request $request
     * @return type
     */
    public function findOrder(Request $request) {
        if (!$request->has('reciver')) {
            return redirect("admin/order/index");
        }
        $orders = \App\Models\Order::where("phone", trim($request->input('reciver')))->orderBy('status', 'asc')->orderBy('time', 'desc')->paginate();
        $msg = '';
        if (!count($orders)) {
            $msg = '没有订单';
        }
        $plots = \App\Models\Plot::where('status', 1)->get();
        $append = $request->all();
        $reciver = $request->input('reciver');
        $assign = compact('orders', 'append', 'msg', 'reciver', 'plots', 'request');
        return view('admin.order.index', $assign);
    }
    
    /**
     * 修改订单
     */
    public function edit(Request $request, $id, $status=false) {
        $order = \App\Models\Order::find($id);
        if (!$order) {
            return \Illuminate\Support\Facades\Response::json([
                'status'    => 0,
                'info'      => '商品不存在'
            ]);
        }
        if (in_array($status, array(2))) {
            $order->status = $status;
            $order->admintime = time();
            $order->adminuser = session('admin')['name'];
            $order->save();
            return \Illuminate\Support\Facades\Response::json([
                'status'    => 1,
                'info'      => '操作成功'
            ]);
        }
        if ($request->ajax() && in_array($status, array(0))) {
			if ($order->status != 0) {
				return \Illuminate\Support\Facades\Response::json([
					'status'    => 0,
					'info'      => '订单不能修改'
				]);
			}
			$adminmod = intval($request->input('adminmod'));
			$facprice = $order->price - $order->pfav - $order->plotfav + $order->plotinit;
			if ($adminmod > $facprice || $adminmod == $facprice) {
				return \Illuminate\Support\Facades\Response::json([
					'status'    => 0,
					'info'      => '操作违法，后台优惠大于或等于产品价格'
				]);
			}
            $order->adminmod = $adminmod;
            $order->admintime = time();
            $order->mark = $request->input('mark');
            $order->adminuser = session('admin')['name'];
            $order->save();
            return \Illuminate\Support\Facades\Response::json([
                'status'    => 1,
                'info'      => '操作成功'
            ]);
        }
        
        if ($request->ajax() && in_array($status, array(3, 4))) {
            $res = \App\Models\Netuser::addNetUser($request, $order->uid, $order->id, $order->type);
            if ($res['status'] == 0) {
                return \Illuminate\Support\Facades\Response::json($res);
            }
            $order->status = $status;
            $order->admintime = time();
            $order->adminuser = session('admin')['name'];
            $order->save();
            return \Illuminate\Support\Facades\Response::json($res);
        }
        $assign = compact('order', 'status');
        return view('admin.order.edit', $assign);
    }
    /**
     *删除订单 
     */
    public function del($id) {
        $order = \App\Models\Order::find($id);
        if ($order && $order->status == 2) {
            $order->delete();
            $res = ['status'=>1, 'info'=>'删除成功'];
        } else {
            $res = ['status'=> 0, 'info'=> '删除失败'];
        }
        return \Illuminate\Support\Facades\Response::json($res);
    }
    /**
     * 报表
     * @param Request $request
     */
    public function report(Request $request) {
        $orders = \App\Models\Order::orderBy('status', 'asc')->orderBy('time', 'desc')->whereIn('status', [1, 3, 4]);
        if ($request->has('plot')) {
            $orders->where('plotid', $request->input('plot'));
        }
        if ($request->has('ptype')) {
            $orders->where('type', $request->input('ptype'));
        }
        if ($request->has('starttime')) {
            $orders->where('time', '>', strtotime($request->input('starttime')));
        }
        if ($request->has('endtime')) {
            $orders->where('time', '<', strtotime($request->input('endtime')));
        }
        $orders = $orders->get();
        $msg = '';
        if (!count($orders)) {
            $msg = '没有该种类订单';
        }
        $plots = \App\Models\Plot::where('status', 1)->get();
        $append = $request->all();
        $assign = compact('orders', 'append', 'msg', 'plots', 'request');
        return view('admin.order.report', $assign);
    }
}
